package member.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import member.model.service.MemberService;
import member.to.ActionForward;
import member.to.MemberTO;

public class ModifyMemberController implements Controller {
	
	@Override
	public ActionForward handle(HttpServletRequest request,
			HttpServletResponse response){
		ActionForward af = null;
		HttpSession session = request.getSession();
		MemberTO loginInfo = (MemberTO)session.getAttribute("login_info");
		if(loginInfo == null){//로그인 안된 경우
			request.setAttribute("error_message", "로그인 후 회원정보 수정이 가능합니다. 로그인 먼저 하세요");
			af = new ActionForward("/login_form.jsp", false);
		}else{
			//3. 회원 정보 수정 처리
			//1. 요청 파라미터 조회
//			String password = request.getParameter("password");
//			String name= request.getParameter("name");
//			String registerNumber1 = request.getParameter("registerNumber1");
//			String registerNumber2 = request.getParameter("registerNumber2");
//			String id = loginInfo.getId();
//			MemberDTO mto = new MemberDTO(id, password, name, registerNumber1, registerNumber2, 0);
			MemberTO mto = (MemberTO)request.getAttribute("mto");
			try {
				MemberService mms = MemberService.getInstance();
				mms.modifyMember(mto);
				af = new ActionForward("/member_detail.jsp", false);
				loginInfo.setPassword(mto.getPassword());
				loginInfo.setName(mto.getName());
				loginInfo.setRegisterNumber1(mto.getRegisterNumber1());
				loginInfo.setRegisterNumber2(mto.getRegisterNumber2());
			} catch (Exception e) {
				e.printStackTrace();
				request.setAttribute("error_message", e.getMessage());
				af = new ActionForward("/error.jsp", false);
			}
		}
		return af;
	}

}
